package ws.db.sql;

import ws.db.ColumnValuePair;
import java.util.ArrayList;
import java.util.Iterator;

abstract public class SQL implements SQLInterface{

	protected String table;
	protected String where;
	protected String order;
	
	public SQL(String m_table){
		table = m_table;
	}
	
	public SQL(String m_table, String m_where){
		this(m_table);
		where = m_where;
	}
	
	public SQL(String m_table, String m_where, String m_order){
		this(m_table,m_where);
		order = m_order;
	}
	
	/**
	 * GetSQL is a polymorphism function.
	 * This provoid a void function.
	 * 
	 * @param:
	 * 		void
	 * @return:
	 * 		String - null.
	 * */
	 public String getSQL(){ return null;}
	
	/**
	 * Add the columns-name into the SQL when
	 * given the columns-name which decrible the retrieve columns.
	 * This function more be used in select SQL.
	 * @Params: 
	 * 		sql -  the SQL which you have partly constucted.
	 * 		m_columns - the column-value Pairs  which contains the columns you will retrieve
	 * @Return:
	 * 		void - The SQL  has been append the columns-name .
	 * */
	protected void constructColumns(StringBuffer sql,ArrayList<ColumnValuePair> m_columns){
		if(m_columns==null){
				return;
		}
		String[] columns = new String[m_columns.size()];
		Iterator its = m_columns.iterator();
		int i=0;
		while(its.hasNext()){
			ColumnValuePair eachpair = (ColumnValuePair)its.next();
			columns[i++] = eachpair.column;
			
		}
		constructColumns(sql,columns);
	}
	
	/**
	 * Select SQL needn`t use the values so the second param is String array.
	 * @param:
	 * 		sql - the SQL which you have partly constucted.
	 * 		columns - the column-name array.
	 * @return:
	 * 		void.
	 * */
	protected void constructColumns(StringBuffer sql,String[] columns){
		
		if(columns==null){
			sql.append(" * ");
		}
		for(int i=0; i<columns.length; i++)
		{
			sql.append(columns[i]);
			if(i!=columns.length-1)
				sql.append(",");
		}
	}
	
	
	
	
	
	
	/**
	 * Deal with the SQL which has where and order clauses.
	 * append the clauses to ths SQL.
	 * @Param:
	 * 		sql -  the SQL which you have constucted.
	 * 		where - the "where" clause when you retrieve the db table.
	 * 		order - the "order" clause when you retrieve the db table.
	 * @Return:
	 * 		void -  The SQL  has been append the where and order clauses.
	 * */
	protected void dealwithWhereOrder(StringBuffer sql,String where, String order){
		
		if(where!=null&&where.length()>0){
			sql.append(" "+where+" ");
		}
		if(order!=null&&order.length()>0){
			sql.append(" "+order+" ");
		}
		
		return ;
	}
	
	/**
	 * Deal with the SQL which has where and order clauses.
	 * Append the clauses to ths SQL.
	 * @Param:
	 * 		sql -  the SQL which you have constucted.
	 * 		where - the "where" clause when you retrieve the db table.
	 * @Return:
	 * 		void - The SQL  has been append the where clauses.
	 * */
	protected void dealwithWhereOrder(StringBuffer sql,String where){
		
		if(where!=null&&where.length()>0){
			sql.append(" "+where+" ");
		}	
		return ;
	}
	
	
}
